import { createStore } from 'vuex'

export default createStore({
  state: {
    logined:false,
    token:null,
    userNick:null
  },
  getters: {
    isLogin (state) {
      if (!state.logined) {    
          state.logined=sessionStorage.getItem('logined');   //从sessionStorage中读取状态
          state.token=sessionStorage.getItem('token');
      }
      return state.logined
    } 
  },
  mutations: {
    LOGIN (state,token) {
      // 登录
      // 先让登录状态变为登录了
      // 若只是改变state里的值，在强制刷新后会丢失，数据添加到sessionStorage里
      sessionStorage.setItem("token", token)  
      sessionStorage.setItem("logined", true) 
      state.token = token
      state.logined = true
      localStorage.setItem("token",token)
     },
     LOGOUT (state) {
      // 这个同理
      sessionStorage.removeItem("token")
      sessionStorage.removeItem("logined")
      state.token = ''
      state.logined = false
      localStorage.removeItem("token")
      localStorage.removeItem("userNick")
     },
     SETLOGINFO(state,userNick){
      localStorage.setItem("userNick", userNick) 
      state.userNick = userNick
     }
    
  },
  actions: {
  },
  modules: {
  }
})
